package hotgammon.view.tools;

import java.awt.event.MouseEvent;

import hotgammon.domain.Color;
import hotgammon.view.BackgammonUserInterface;
import minidraw.framework.Tool;
import minidraw.standard.NullTool;

public class GameStateTool extends NullTool {
	
	private BackgammonUserInterface bu;
	private Tool move, die, win;
	
	public GameStateTool(Tool move, Tool die, Tool win, BackgammonUserInterface bu) {
		this.move = move;
		this.die = die;
		this.win = win;
		this.bu = bu;
	}
	
	public void mouseDown(MouseEvent e, int x, int y) {
		getState().mouseDown(e, x, y);
	}
	
	public void mouseDrag(MouseEvent e, int x, int y) {
		getState().mouseDrag(e, x, y);
	} 
	
	public void mouseUp(MouseEvent e, int x, int y) {
		getState().mouseUp(e, x, y);
		printMessage();
	}
	
	private void printMessage() {
		if(getState() == die)
			bu.getEditor().showStatus("Roll dice !");
		else if(getState() == move)
			bu.getEditor().showStatus("Player " + bu.getGame().getPlayerInTurn() + ": " + bu.getGame().getNumberOfMovesLeft() + " move(s) left");
		else
			bu.getEditor().showStatus("The winner is " + bu.getGame().winner() + " !");
	}
	
	private Tool getState() {
		if(bu.getGame().winner() != Color.NONE)
			return win;
		else if(bu.getGame().getNumberOfMovesLeft() == 0)
			return die;
		else
			return move;
	}
}
